﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Collections.Generic;

namespace AspCompiler
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            comboBox1.DataSource = Config.WebRoot;
            comboBox1.SelectedIndex = 0;
            comboBox1_SelectedIndexChanged(null, null);

            if (Config.WebRoot.Length < 2)
            {
                comboBox1.Visible = false;
                listBox1.Location = new Point(listBox1.Location.X, listBox1.Location.Y - 40);
                listBox1.Size = new Size(listBox1.Size.Width, listBox1.Size.Height + 40);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox1.Items.Clear();

            //prevent error on application start when there is no directories in webroot
            if (!Directory.Exists(comboBox1.SelectedItem.ToString()))
                return;

            foreach (DirectoryInfo dir in new DirectoryInfo(comboBox1.SelectedItem.ToString()).GetDirectories())
            {
                if ((dir.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
                    listBox1.Items.Add(dir.Name);
            }
        }

        CmdProcessor cmd = null;
        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "Stop")
            {
                if (cmd != null)
                    cmd.Cancel();
                button1.Text = "Compile";
                return;
            }

            //prevent error when no directory is selected when compile button is clicked
            if (listBox1.SelectedItem == null)
                return;

            textBox1.Text = String.Empty;
            string d = listBox1.SelectedItem.ToString();
            string root = comboBox1.SelectedItem + d + "\\";
            button1.Text = "Stop";

            if (!File.Exists(Config.Compiler(root)))
            {
                textBox1.Text = "Error: compiler tool does not exist at specified path, please check your app.config settings.";
                return;
            }

            cmd = new CmdProcessor();
            cmd.TextReceived += new CmdProcessorEventHandler(cmd_TextReceived);
            cmd.Finished += new EventHandler(cmd_Finished);
            cmd.QueueCommand(Config.Compiler(root), String.Format(Config.CompilerArgs(root), "\"" + comboBox1.SelectedItem + d + "\"", "\"" + Config.PubRoot + d + "\""));
            if (File.Exists(Config.Merge(root)))
                cmd.QueueCommand(Config.Merge(root), String.Format(Config.MergeArgs(root), "\"" + Config.PubRoot + d + "\"", (d.Contains(".") ? d.Substring(0, d.IndexOf(".")) : d)));
            cmd.Execute();
        }

        private void cmd_TextReceived(object sender, CmdProcesserEventArgs e)
        {
            if (InvokeRequired)
                Invoke(new CmdProcessorEventHandler(cmd_TextReceived), sender, e);
            else
                textBox1.AppendText(e.Output);
        }

        private void cmd_Finished(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(new EventHandler(cmd_Finished), sender, e);
                return;
            }

            string path = Config.PubRoot + listBox1.SelectedItem + "\\";

            //delete web.config and rename web.config.live to web.config
            if (File.Exists(path + "web.config.live"))
            {
                File.Delete(path + "web.config");
                File.Move(path + "web.config.live", path + "web.config");
            }

            if (File.Exists(path + "compiler.config"))
                File.Delete(path + "compiler.config");

            //delete any xml for dll files which just store the method's help usage text
            if (Directory.Exists(path + "bin"))
            {
                foreach (string file in Directory.GetFiles(path + "bin", "*.xml"))
                {
                    if (File.Exists(file.Substring(0, file.Length - 3) + "dll"))
                        File.Delete(file);
                }
            }

            button1.Text = "Compile";
        }
    }
}
